<?php
/**
 * Description of SearchController
 *
 * @author Mirasoft
 */

Class SearchController{
public $title;
public $index;
public $search = true;
public $layout;
public $input;
public $results;
 
	public function __construct(){
		LoadHelpers("Use");
		
		require_once(LIBRARY_PATH.'/Kernel/Cache/Cache.php');
		$cache = DF_cache::Create()->SetResource('Object')->SetName('menu');
		
		$cat = $cache->Get();
		
		if($cat==null){
			
			$data = ORM::factory()->SetType('assoc')->prepare("SELECT GroupTovar.name, GroupTovar.url, CetegoryTovar.name as `category`, CetegoryTovar.url as `curl` FROM `GroupTovar` LEFT JOIN `CetegoryTovar` ON GroupTovar.id = CetegoryTovar.group")->getResult();
			
			$cat = array();
			
			foreach($data as $item){
				$cat[$item['url']][] = array('name'=>$item['name'],'category'=>$item['category'],'curl'=>$item['curl']);
			}
			$cache->SetData($cat);
			$cache->SetResource('Object')->SetName('menu')->Save();
		}
		$this->cat = $cat;
		
		unset($data);
		unset($cat);		
		$this->new_goods = ORM::factory()->SetType('assoc')->prepare("SELECT goods.id, goods.url, goods.name, goods.small, GroupTovar.url as `group`, CetegoryTovar.url as `cat` FROM `goods`, `GroupTovar`, `CetegoryTovar` WHERE goods.Sub=CetegoryTovar.id AND CetegoryTovar.group = GroupTovar.id LIMIT 0,5")->getResult();
	}

	public function IndexAction(){
        $this->keywords = NULL;
		$this->description = NULL;

		$this->title = "Пошук";
		
		$array = array();
			$array[] = array('link'=>'index','name'=>'Головна');			
			$request = getRequest::getInstance();
			if($request->isGet()){
				$this->input = $request->getVar('search', 'GET');
				if(!empty($this->input)){
					$array[] = array('link'=>'search','name'=>'Пошук', 'param'=>$this->input, 'type'=>'input');
					$cache = DF_cache::Create()->SetResource('Object')->SetName('search_'.urlencode($this->input));
					$this->results = $cache->Get();
					if($this->results==null){
						$this->results = array();
						$this->results['content'] = ORM::factory()->SetType('assoc')->prepare("SELECT razdel.id, razdel.title, razdel.url FROM `razdel`,`content` WHERE MATCH (razdel.title, razdel.keywords, razdel.description, content.body, content.descrip) AGAINST ('".$this->input."' IN BOOLEAN MODE) AND razdel.id = content.razdel")->getResult();
						$this->results['goods'] = ORM::factory()->SetType('assoc')->prepare("SELECT goods.id, goods.name, goods.url, CetegoryTovar.url as `cat`, GroupTovar.url as `group` FROM `goods`,`CetegoryTovar`, `GroupTovar` WHERE MATCH (goods.name, goods.descrip, goods.materіal, goods.color) AGAINST ('".$this->input."' IN BOOLEAN MODE) AND goods.Cat = CetegoryTovar.id AND CetegoryTovar.group = GroupTovar.id")->getResult();
						$cache->SetData($this->results);
						$cache->SetResource('Object')->SetName('search_'.urlencode($this->input))->Save();
					}
				}
			}else{
				$array[] = array('link'=>'search','name'=>'Пошук');
			}	
			$this->link = LinkGen::Link($array, 'block', 'sitemap');
	}
	
	public function GetAction(){
		$this->layout = 'false';
		
		$request = getRequest::getInstance();
		
		if($request->isGet()==true){
			//$value = "сервет";
			$value = $request->getVar('value', 'GET');
			//$sql = "SELECT goods.name, goods.small, goods.url, CetegoryTovar.url as `cat`, GroupTovar.url as `group`  FROM `goods`,`CetegoryTovar`, `GroupTovar` WHERE MATCH (goods.name, goods.descrip, goods.materіal, goods.color) AGAINST ('".$value."') AND goods.Cat = GroupTovar.id AND goods.Sub=CetegoryTovar.id AND CetegoryTovar.group = GroupTovar.id";
			$search = substr($value, 0, 64);
			$search = preg_replace("/[^\w\x7F-\xFF\s]/", " ", $search);
			$good = trim(preg_replace("/\s(\S{1,3})\s/", " ", ereg_replace(" +", "  ",$search)));
			//echo"TRIM : ".$good."<br>";
			unset($search);
			unset($value);
			$good = ereg_replace(" +", " ", $good);
			$tmp = explode(" ", $good);
			unset($good);
			$size = sizeof($tmp);
			$tmp[$size-1] = $tmp[$size-1].'*';
			$good = implode(' ', $tmp);
			unset($tmp);
			
			//echo"SEARCH : ".$good."<br>";
			$sql = "SELECT goods.name, goods.small, goods.url, CetegoryTovar.url as `cat`, GroupTovar.url as `group`  FROM `goods`,`CetegoryTovar`, `GroupTovar` WHERE MATCH (goods.name, goods.descrip, goods.materіal, goods.color) AGAINST ('".$good."' IN BOOLEAN MODE) AND goods.Cat = GroupTovar.id AND goods.Sub=CetegoryTovar.id AND CetegoryTovar.group = GroupTovar.id LIMIT 10";
			//$sql = "SELECT goods.name, goods.small, goods.url, CetegoryTovar.url as `cat`, GroupTovar.url as `group`  FROM `goods`,`CetegoryTovar`, `GroupTovar` WHERE MATCH (goods.name, goods.descrip, goods.materіal, goods.color) AGAINST ('".$value."' IN BOOLEAN MODE) AND goods.Cat = CetegoryTovar.id AND CetegoryTovar.group = GroupTovar.id";
			//echo $sql;
			$result = ORM::factory()->SetType('assoc')->prepare($sql)->getResult();
			/*echo"<pre>";
			print_r($result);
			echo"</pre>";*/
			$callback = getRequest::getInstance()->getVar('callback','GET');
			$json = $callback."(".json_encode($result).")";

			header('Content-Type: application/json; charset=utf-8');
			
			echo $json;
		}
	}
	
}
